package com.crm.service.impl.cluePoolManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.cluePoolManager.CluePoolPublicMapper;
import com.crm.dao.mapper.customMapper.cluePoolManagerCustomMapper.CluePoolCustomMapper;
import com.crm.model.entity.cluePoolManager.CluePoolPublic;
import com.crm.model.entity.cluePoolManager.CluePoolPublicExample;
import com.crm.model.qo.cluePoolManager.CluePoolOpenSeaQO;
import com.crm.model.vo.cluePoolManager.CluePoolPubliclOpenSeaVO;
import com.crm.redis.service.cluePoolManager.CluePoolPublicRedisService;
import com.crm.service.cluePoolManager.CluePoolPublicDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author yincuimei
 * @Date 2020/12/24 11:16
 */
@Service
public class CluePoolPublicDbServiceImpl implements CluePoolPublicDbService {

    @Autowired
    private CluePoolPublicMapper cluePoolPublicMapper;

    @Autowired
    private CluePoolCustomMapper cluePoolCustomMapper;

    @Autowired
    private CluePoolPublicRedisService cluePoolPublicRedisService;

    @Override
    public Boolean insertCluePoolPublic(CluePoolPublic cluePoolPublic) {
        Boolean result = false;
        int num = cluePoolPublicMapper.insertSelective(cluePoolPublic);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 根据线索池ID删除线索池(私有池、分公司公有池、全国池子)--逻辑删除
     * @param cluePoolPublic ---线索池信息
     * @return
     */
    @Override
    public Boolean deleteCluePoolPublic(CluePoolPublic cluePoolPublic) {
        //更新公共线索池信息，更新成功后产出公共线索池redis信息
        int num = cluePoolPublicMapper.updateByPrimaryKeySelective(cluePoolPublic);
        if(num>0){
            cluePoolPublicRedisService.deleteCluePoolPublicById(cluePoolPublic.getCluePoolPublicId());
        }
        return num > 0;
    }

    @Override
    public CluePoolPublic selectCluePoolPublicById(String cluePoolId) {
        //先在redis中查询公共线索池信息，若没有则在数据库中查询，插入到redis中
        CluePoolPublic cluePoolPublic= cluePoolPublicRedisService.getCluePoolPublicById(cluePoolId);
        if (cluePoolPublic == null) {
            cluePoolPublic = cluePoolPublicMapper.selectByPrimaryKey(cluePoolId);
            if (cluePoolPublic != null) {
                cluePoolPublicRedisService.setCluePoolPublicById(cluePoolPublic);
            }
        }
        return cluePoolPublic;
    }

    @Override
    public CluePoolPublic selectCluePoolPublicByName(String cluePoolName) {
        CluePoolPublicExample cluePoolPublicExample = new CluePoolPublicExample();
        CluePoolPublicExample.Criteria criteria = cluePoolPublicExample.createCriteria();
        criteria.andCluePoolNameEqualTo(cluePoolName);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CluePoolPublic> cluePoolPublicList =  cluePoolPublicMapper.selectByExample(cluePoolPublicExample);
        if(cluePoolPublicList.size()>0){
            return cluePoolPublicList.get(0);
        }else {
            return null;
        }
    }

    @Override
    public CluePoolPublic selectCluePoolPublicUpdateByNameAndId(String cluePoolName, String cluePoolId) {
        CluePoolPublicExample cluePoolPublicExample = new CluePoolPublicExample();
        CluePoolPublicExample.Criteria criteria = cluePoolPublicExample.createCriteria();
        criteria.andCluePoolNameEqualTo(cluePoolName);
        criteria.andCluePoolPublicIdNotEqualTo(cluePoolId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CluePoolPublic> cluePoolPublicList =  cluePoolPublicMapper.selectByExample(cluePoolPublicExample);
        if(cluePoolPublicList.size()>0){
            return cluePoolPublicList.get(0);
        }else {
            return null;
        }
    }

    /**
     * @author wll
     * info 用户模块-线索池管理-线索池公海表单 (1-全国 2-公司 3-我参与的)
     * @return
     */
    @Override
    public List<CluePoolPubliclOpenSeaVO> selectCluePoolPublicInfo(Map map) {
        return cluePoolCustomMapper.selectCluePoolPublicInfo(map);
    }

    @Override
    public Boolean updateCluePoolPublicById(CluePoolPublic cluePoolPublic) {
        //更新公共线索池信息，更新成功后产出公共线索池redis信息
        int num = cluePoolPublicMapper.updateByPrimaryKeySelective(cluePoolPublic);
        if(num>0){
            cluePoolPublicRedisService.deleteCluePoolPublicById(cluePoolPublic.getCluePoolPublicId());
        }
        return num>0;
    }

    /**
     * 查询全国默认池的信息
     * @return
     */
    @Override
    public CluePoolPublic selectNationalDefaultPoolInfo() {
        CluePoolPublicExample cluePoolPublicExample = new CluePoolPublicExample();
        CluePoolPublicExample.Criteria criteria = cluePoolPublicExample.createCriteria();
        //nature=3  delete_status=0 pool_state=1 default_mark=1
        criteria.andNatureEqualTo(3);
        criteria.andDeleteStatusEqualTo(Short.parseShort("0"));
        criteria.andPoolStateEqualTo(1);
        criteria.andDefaultMarkEqualTo(1);
        List<CluePoolPublic> cluePoolPublics = cluePoolPublicMapper.selectByExample(cluePoolPublicExample);
        if(cluePoolPublics.size()>0){
            return cluePoolPublics.get(0);
        }else {
            return null;
        }
    }
}
